package com.ruoyi.card.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

import cn.hutool.core.date.DateTime;
import com.ruoyi.card.domain.vo.GetTimeListVo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.rest.domain.vo.TreeNameVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.card.mapper.SillCardMapper;
import com.ruoyi.card.domain.SillCard;
import com.ruoyi.card.service.ISillCardService;

/**
 * 员工打卡表Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-08-12
 */
@Service
public class SillCardServiceImpl implements ISillCardService 
{
    @Autowired
    private SillCardMapper sillCardMapper;

    /**
     * 查询员工打卡表
     * 
     * @param id 员工打卡表主键
     * @return 员工打卡表
     */
    @Override
    public SillCard selectSillCardById(Long id)
    {
        return sillCardMapper.selectSillCardById(id);
    }

    /**
     * 查询员工打卡表列表
     * 
     * @param sillCard 员工打卡表
     * @return 员工打卡表
     */
    @Override
    public List<SillCard> selectSillCardList(SillCard sillCard)
    {
        return sillCardMapper.selectSillCardList(sillCard);
    }

    /**
     * 新增员工打卡表
     * 
     * @param sillCard 员工打卡表
     * @return 结果
     */
    @Override
    public int insertSillCard(SillCard sillCard)
    {
        extracted(sillCard);
        return sillCardMapper.insertSillCard(sillCard);
    }
  //拼接年月
    private static void extracted(SillCard sillCard) {
        Date sillPerformanceTime = sillCard.getSillCardTime();
        //得到日期中的日
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        // 使用ISO_LOCAL_DATE_TIME格式解析日期时间
        String dateTimeString = dateFormat.format(sillPerformanceTime);
        // 将转换为的字符串进行拆分
        String[] split = dateTimeString.split("-");
        // 输出年月日
        sillCard.setSillCardDay(split[0]+"年"+split[1]+"月");
    }

    /**
     * 修改员工打卡表
     * 
     * @param sillCard 员工打卡表
     * @return 结果
     */
    @Override
    public int updateSillCard(SillCard sillCard)
    {

        extracted(sillCard);
        return sillCardMapper.updateSillCard(sillCard);
    }

    /**
     * 批量删除员工打卡表
     * 
     * @param ids 需要删除的员工打卡表主键
     * @return 结果
     */
    @Override
    public int deleteSillCardByIds(Long[] ids)
    {
        return sillCardMapper.deleteSillCardByIds(ids);
    }

    /**
     * 删除员工打卡表信息
     * 
     * @param id 员工打卡表主键
     * @return 结果
     */
    @Override
    public int deleteSillCardById(Long id)
    {
        return sillCardMapper.deleteSillCardById(id);
    }

    @Override
    public int insertDK() throws ParseException {
        String username = SecurityUtils.getUsername();
        SillCard sillCard = new SillCard();
        sillCard.setSillCardNum(username);
        extracted1(sillCard);
        String name = sillCardMapper.getName(username);
        sillCard.setSillCardName(name);
        extracted(sillCard);
        sillCardMapper.insertSillCard(sillCard);
        return 1;
    }

    @Override
    public Boolean selectTimeList() {
        String username = SecurityUtils.getUsername();
        List<GetTimeListVo> allFloor = sillCardMapper.getTime(username);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String currentTime = sdf.format(date);
        for (GetTimeListVo getTimeListVo : allFloor) {
            Date sillCardTime = getTimeListVo.getSill_card_time();
            String formattedDate = sdf.format(sillCardTime);
            if(formattedDate.equals(currentTime)){
                return false;
            }
        }
        return true;

    }

    //设置打卡时间
    private static void extracted1(SillCard sillCard) throws ParseException {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedDateTime = now.format(formatter);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = sdf.parse(formattedDateTime);
        DateTime dateTime = new DateTime(date);
        sillCard.setSillCardTime(dateTime);
    }
}
